Print screen using java

Is there any api for capure my screen ..?
You don't need any api. Java have the inbuild functionalities for this. Using the Robot class you can print your screen. Here is the code sample and explanation.

Example code:

import java.awt.image.BufferedImage;
import java.awt.Rectangle;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.Robot;
import java.io.File;
import javax.imageio.ImageIO;

class ScreenRecorder {
  public static void main(String args[]) {
    try {
      Toolkit tool = Toolkit.getDefaultToolkit();
      Dimension d = tool.getScreenSize();
      Rectangle rect = new Rectangle(d);
      Robot robot = new Robot();
      Thread.sleep(2000);
      File f = new File("screenshot.jpg");
      BufferedImage img = robot.createScreenCapture(rect);
      ImageIO.write(img,"jpeg",f);
      tool.beep();
    } catch(Exception e){
      e.printStackTrace();
    }
  }
}
  • Import the classes you need.

  • Using the ToolKit you can get your screensize.

  • Form a Rectangle object with this width and height

  • capture the screen for this full width and height.

  • There is a Thread.sleep for 2 sec for my convenience

  • You can specify your own width and height by

    rect = new Rectangle(640,480);

I think this is help you more. Now am working on record the desktop using java. Soon i will come up with good product.

Captured Image:

image0

Show Comments